package com.ai.aicontact.busi.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ai.aicontact.busi.entity.ExecFileLog;
import com.ai.aicontact.busi.entity.RedBlackList;
import com.ai.aicontact.busi.service.ExecFileLogService;
import com.ai.aicontact.common.vo.PageFilter;
import com.ai.aicontact.core.dao.BaseDaoI;

@Service
public class ExecFileLogServiceImpl implements ExecFileLogService{

	@Autowired
	 BaseDaoI<ExecFileLog> execFileLogDao;
	
	@Override
	public List<ExecFileLog> queryExecFileLog(ExecFileLog execFileLog,PageFilter pf) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder hql = new StringBuilder("select new com.ai.aicontact.busi.entity.ExecFileLog(t.logId,t.fileName,t.customerId,t.fileCount,t.fileIndex,t.fileVersion,t.startTime,t.endTime,t.summary,t.success,t.fail,t.state,t.remark) from ExecFileLog t");
		hql.append(whereHql(execFileLog, params));
		hql.append(orderHql(pf)); 
		List<ExecFileLog> list = execFileLogDao.find(hql.toString(), params, pf.getPageNumber(), pf.getPageSize());
		return list;
	}

	private String whereHql(ExecFileLog execFileLog, Map<String, Object> params) {
		StringBuilder hql = new StringBuilder(" where 1=1");
		
		if (!("".equals(execFileLog.getFileName())) && execFileLog.getFileName() != null) {
			hql.append(" AND fileName like :fileName");
			params.put("fileName", "%"+execFileLog.getFileName()+"%");
		}
		
		if (!("".equals(execFileLog.getCustomerId()))  && execFileLog.getCustomerId() != null) {
			hql.append(" AND customerId like :customerId");
			params.put("customerId", "%"+execFileLog.getCustomerId()+"%");
			
		}
		if (execFileLog.getState() != null) {
			hql.append(" AND state = :state");
			params.put("state", execFileLog.getState());
		}
		
		if (execFileLog.getExecFileLogDateStart() != null) {
			hql.append(" AND startTime >= :execFileLogDateStart");
			params.put("execFileLogDateStart", execFileLog.getExecFileLogDateStart());
		}
		
		if (execFileLog.getExecFileLogDateEnd() != null) {
			hql.append(" AND startTime <= :execFileLogDateEnd");
			params.put("execFileLogDateEnd", execFileLog.getExecFileLogDateEnd());
		}
		return hql.toString();
	}
	
	@Override
	public Long countExecFileLog(ExecFileLog execFileLog) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder hql = new StringBuilder("select count(*) from ExecFileLog ");
		hql.append(whereHql(execFileLog, params));
		Long total = execFileLogDao.count(hql.toString(), params);
		return total;
	}
	
	private String orderHql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by " + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}

}
